//import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutterApp/api/Api.dart';
//import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../common//Button.dart';

class LoginPage extends StatefulWidget {
  LoginPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  BuildContext context;
  GlobalKey<FormState> formGlobalKey = GlobalKey<FormState>();
  String userName;
  String pwd;
  void loginFun() async {
    var form = formGlobalKey.currentState;
    if (form.validate()) {
      form.save();
      print(userName + "," + pwd);
      Response response =
          await getStatus({'account': this.userName, 'password': this.pwd});
      if (response.data['isCorrect']) {
        Navigator.pushNamed(context, '/home');
      }
    }
    //showOkAlertDialog(context:context,title:"登录成功");
  }

  Widget _createPage(BuildContext context) {
    //EasyLoading.show(status: 'loading...',maskType:EasyLoadingMaskType.clear);
    //EasyLoading.showToast('Toast');
    return Form(
      key: formGlobalKey,
      child: Center(
        child: Container(
          decoration: new BoxDecoration(
            color: Colors.white,
          ),
          child: Column(children: <Widget>[
            Image.asset('images/login_bg.png'),
            Container(
              padding: EdgeInsets.fromLTRB(30, 40, 30, 0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text("用户名"),
                  TextFormField(
                    style:TextStyle(fontSize: 15),
                    decoration: InputDecoration(
                      hintText: "请输入用户名",
                    ),
                    validator: (content) {
                      if (content.isEmpty) {
                        return "请输入用户名";
                      }
                    },
                    onSaved: (value) {
                      userName = value;
                    },
                  ),
                  SizedBox(
                    height: 30,
                  ),
                  Text("密码"),
                  TextFormField(
                    obscureText: true,
                      style:TextStyle(fontSize: 15),
                    decoration: InputDecoration(
                      hintText: "请输入密码",
                    ),
                    validator: (content) {
                      if (content.isEmpty) {
                        return "请输入密码";
                      }
                    },
                    onSaved: (value) {
                      pwd = value;
                    },
                  ),
                  SizedBox(
                    height: 40,
                  ),
                  Button(name: '登录', callback: this.loginFun)
                ],
              ),
            )
          ]),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    this.context = context;
    return Container(
        child: Scaffold(
            appBar: AppBar(
              title: Text("绑定用户"),
              centerTitle: true,
              // backgroundColor:Color(0xFF000000)
            ),
            body: SingleChildScrollView(
              child: this._createPage(context),
            )));
  }
}
